class Watcher{
    constructor(){
        this.events = {};
    }
    subscribe(eventName,handler){
        if(!this.events[eventName]){
            this.events[eventName] = [];
        }
        this.events[eventName].push(handler);
        return this;
    }
    publish(eventName,...args){
        if(!this.events[eventName]){
            return ;
        }
        this.events[eventName].forEach(func=>{
            func(...args);
        })
        return this;
    }
    remove(eventName,handler){
        if(!this.events[eventName]){
            return;
        }
        let index = -1;
        this.events[eventName].forEach((func,curindex)=>{
            if(func === handler){
                index = curindex;
            }
        })
        this.events[eventName].splice(index,1);
        return this;
    }
    removeAll(eventName){
        if(!this.events[eventName]) return;
        this.events[eventName] = null;
    }

}